package com.ssm.user.event;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class PersonEventPublisher {

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher; //spring容器初始化时已加载，可直接使用注解注入

    public void createPerson(Person person) {
        //想要发布事件，对象就要是ApplicationEvent类型
        applicationEventPublisher.publishEvent(new PersonChangeEvent(person, "create"));
    }

    public void updatePerson(Person person) {
        applicationEventPublisher.publishEvent(new PersonChangeEvent(person, "update"));
    }
}
